package com.todo.biz.application.handler.bizData;

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Maps;
import com.todo.biz.application.entity.context.SimpleBusinessDataContext;
import com.todo.biz.application.remote.UserInfoRemoteService;
import com.todo.biz.application.remote.UserStrategyRemoteService;
import com.todo.common.common.entity.response.BizResponse;
import com.todo.common.common.enums.BusinessSceneEnum;
import com.todo.strategy.online.service.api.response.UserStrategyBaseResponse;
import com.todo.user.api.response.BatchQuerySimplyUserInfoResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.MapUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author azhebuxing
 * @date 2024/5/30 11:13
 * @description
 */
@Slf4j
@Component
public class UserStrategyDataHandler extends AbstractBusinessDataHandle {

    @Resource
    private UserStrategyRemoteService userStrategyRemoteService;
    @Resource
    private UserInfoRemoteService userInfoRemoteService;


    @Override
    protected Map<Long, SimpleBusinessDataContext> doQueryBizData(List<Long> bizIdList) {
        if (CollectionUtils.isEmpty(bizIdList)) {
            return Maps.newHashMap();
        }
        BizResponse<List<UserStrategyBaseResponse>> bizResponse = userStrategyRemoteService.queryUserStrategyList(bizIdList);
        if (bizResponse == null || !bizResponse.isSuccess() || CollectionUtils.isEmpty(bizResponse.getData())) {
            log.warn("UserStrategyDataHandler#doQueryBizData 根据主键查询内容详情完成，数据为空 入参：【{}】", JSONObject.toJSONString(bizIdList));
            return Maps.newHashMap();
        }
        List<Long> userIdList = bizResponse.getData().stream().map(UserStrategyBaseResponse::getUserId).collect(Collectors.toList());
        BizResponse<BatchQuerySimplyUserInfoResponse> response = userInfoRemoteService.batchQuerySimplyUserInfo(userIdList);
        if (response == null || !response.isSuccess() || MapUtils.isEmpty(response.getData().getSimplyUserInfoMap())) {
            log.warn("UserStrategyDataHandler#doQueryBizData 根据主键查询用户信息完成，数据为空 入参：【{}】", JSONObject.toJSONString(bizIdList));
            return Collections.emptyMap();
        }
        Map<Long, BatchQuerySimplyUserInfoResponse.SimplyUserInfo> simplyUserInfoMap = response.getData().getSimplyUserInfoMap();

        Map<Long, SimpleBusinessDataContext> resultMap = Maps.newHashMap();
        for (UserStrategyBaseResponse userStrategyInfoResponse : bizResponse.getData()) {
            if (userStrategyInfoResponse == null || !simplyUserInfoMap.containsKey(userStrategyInfoResponse.getUserId())) {
                continue;
            }

            SimpleBusinessDataContext context = new SimpleBusinessDataContext();
            context.setBizId(String.valueOf(userStrategyInfoResponse.getId()));

            BatchQuerySimplyUserInfoResponse.SimplyUserInfo simplyUserInfo = simplyUserInfoMap.get(userStrategyInfoResponse.getUserId());
            SimpleBusinessDataContext.UserInfo userInfo = new SimpleBusinessDataContext.UserInfo();
            userInfo.setUserId(String.valueOf(userStrategyInfoResponse.getUserId()));
            userInfo.setUserNikeName(simplyUserInfo.getUserNikeName());
            userInfo.setUserAvatar(simplyUserInfo.getUserAvatar());
            userInfo.setLocation(simplyUserInfo.getLocation());
            context.setUserInfo(userInfo);
            context.setTitle(userStrategyInfoResponse.getTitle());
            context.setMainImg(userStrategyInfoResponse.getMainImg());
            context.setLikeCount(userStrategyInfoResponse.getLikeCount());
            context.setCommentCount(userStrategyInfoResponse.getCommentCount());
            context.setCollectCount(userStrategyInfoResponse.getCollectCount());
            context.setBusinessScene(BusinessSceneEnum.STRATEGY);
            resultMap.put(userStrategyInfoResponse.getId(), context);

        }
        return resultMap;
    }

    @Override
    public BusinessSceneEnum getHandleBusinessScene() {
        return BusinessSceneEnum.STRATEGY;
    }
}
